package com.rede.didiok.admin.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.admin.dao.MessagePushDao;
import com.rede.didiok.admin.entity.MessagePushEntity;
import com.rede.didiok.admin.service.MessagePushService;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.domainEvent.EventAction;
import com.rede.didiok.common.dto.user.MessagePushDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.exception.exceptionType.QueryException;
import com.rede.didiok.common.holder.RequestHolder;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.common.utils.business.DomainEventUtil;

/**
 * 博客分类表 服务实现类
 *
 * @author 陌溪
 * @since 2018-09-08
 */
@Service
public class MessagePushServiceImpl extends ServiceImpl<MessagePushDao, MessagePushEntity> implements MessagePushService {

    @Resource
    private DomainEventUtil domainEventUtil;

    /***************************** admin:start **************************/
    // region  didiok-admin

    @Override
    public IPage<MessagePushEntity> getPageList(MessagePushDto messagePushDto) {
        QueryWrapper<MessagePushEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(messagePushDto.getKeyword()) && !StringUtils.isEmpty(messagePushDto.getKeyword().trim())) {
            queryWrapper.like(SQLConf.TITLE, messagePushDto.getKeyword().trim());
        }
        if (messagePushDto.getPushMethod() != null) {
            queryWrapper.like(SQLConf.PUSH_METHOD, messagePushDto.getPushMethod());
        }
        if (StringUtils.isNotEmpty(messagePushDto.getOrderByAscColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(messagePushDto.getOrderByAscColumn())).toString();
            queryWrapper.orderByAsc(column);
        } else if (StringUtils.isNotEmpty(messagePushDto.getOrderByDescColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(messagePushDto.getOrderByDescColumn())).toString();
            queryWrapper.orderByDesc(column);
        } else {
            queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        }
        Page<MessagePushEntity> page = new Page<>();
        page.setCurrent(messagePushDto.getCurrentPage());
        page.setSize(messagePushDto.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        IPage<MessagePushEntity> pageList = this.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public String addMessagePush(MessagePushDto messagePushDto) {
        MessagePushEntity messagePush = new MessagePushEntity();
        // 使用Spring工具类提供的深拷贝进行实体类赋值
        BeanUtils.copyProperties(messagePushDto, messagePush, SysConf.STATUS);
        messagePush.setAdminUid(RequestHolder.getAdminUid());
        messagePush.insert();
        // 发送触达变更领域事件
        MessagePushDto dto = new MessagePushDto();
        BeanUtils.copyProperties(messagePush, dto);
        domainEventUtil.publishEvent(EventAction.MESSAGE_PUSH_ADD, dto);
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String messagePush(MessagePushDto messagePushDto) {
        // 发送触达变更领域事件
        MessagePushEntity messagePush = this.getById(messagePushDto.getUid());
        if (messagePush == null) {
            throw new QueryException(MessageConf.ENTITY_NOT_EXIST);
        }
        MessagePushDto dto = new MessagePushDto();
        BeanUtils.copyProperties(messagePush, dto);
        domainEventUtil.publishEvent(EventAction.MESSAGE_PUSH, dto);
        return ResultUtil.successWithMessage("推送成功");
    }

    @Override
    public String editMessagePush(MessagePushDto messagePushDto) {
        MessagePushEntity messagePush = this.getById(messagePushDto.getUid());
        // 使用Spring工具类提供的深拷贝进行实体类赋值
        BeanUtils.copyProperties(messagePushDto, messagePush, SysConf.STATUS);
        messagePush.setUpdateTime(new Date());
        messagePush.setAdminUid(RequestHolder.getAdminUid());
        messagePush.updateById();
        // 发送触达变更领域事件
        MessagePushDto dto = new MessagePushDto();
        BeanUtils.copyProperties(messagePush, dto);
        domainEventUtil.publishEvent(EventAction.MESSAGE_PUSH_EDIT, dto);
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String deleteBatchMessagePush(List<MessagePushDto> messagePushVoList) {
        if (messagePushVoList.size() == 0) {
            return ResultUtil.errorWithMessage(MessageConf.PARAM_INCORRECT);
        }
        List<String> uids = new ArrayList<>();

        messagePushVoList.forEach(item -> {
            uids.add(item.getUid());
        });

        Collection<MessagePushEntity> messagePushList = this.listByIds(uids);
        messagePushList.forEach(item -> {
            item.setUpdateTime(new Date());
            item.setStatus(StatusEnum.DISABLED);
            MessagePushDto dto = new MessagePushDto();
            BeanUtils.copyProperties(item, dto);
            domainEventUtil.publishEvent(EventAction.MESSAGE_PUSH_DELETE, dto);
        });
        Boolean save = this.updateBatchById(messagePushList);
        if (save) {
            return ResultUtil.successWithMessage(MessageConf.DELETE_SUCCESS);
        } else {
            return ResultUtil.errorWithMessage(MessageConf.DELETE_FAIL);
        }
    }

    // endregion
    /***************************** admin:end **************************/



    /***************************** web:start **************************/
    // region didiok-web

    // endregion
    /***************************** web:end **************************/


}
